using Autofac;
using Autofac.Extensions.DependencyInjection;
using EventHandlerDemo.Application;
using EventHandlerDemo.Application.OrderEventLogs.EventHandlers.Orders;
using EventHandlerDemo.Application.Orders;
using EventHandlerDemo.Domain.Orders.Events;
using EventHandlerDemo.Infrastructure;
using EventHandlerDemo.Infrastructure.EFCore;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
{
    builder.RegisterModule<ApplicationModule>();
    builder.RegisterModule<InfrastructureModule>();
});
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddMediatR(configuration =>
{
    configuration.RegisterServicesFromAssemblyContaining<OrderCreatedEvent>();
    configuration.RegisterServicesFromAssemblyContaining<OrderCreatedEventHandler>();
});
builder.Services.AddScoped<OrderAppService, OrderAppService>();
builder.Services.AddDbContext<AppDbContext>(options =>
{
    options.UseMySql(builder.Configuration.GetConnectionString("Default"), MySqlServerVersion.LatestSupportedServerVersion);
});

var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthorization();
app.MapControllers();
app.Run();